package com.mfangsoft.zhuangjialong.app.helperActivity.controller;





import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.facebook.presto.sql.parser.SqlBaseParser.UseContext;
import com.mfangsoft.zhuangjialong.app.entryactivity.model.BaseEntryActivity;
import com.mfangsoft.zhuangjialong.app.entryactivity.service.EntryActivityService;
import com.mfangsoft.zhuangjialong.app.order.model.OrderGift;
import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
import com.mfangsoft.zhuangjialong.common.utils.UserSessionUtil;
import com.mfangsoft.zhuangjialong.integration.activity.model.BaseActivityInfo;
import com.mfangsoft.zhuangjialong.integration.activity.model.BaseActivityInfoItem;
import com.mfangsoft.zhuangjialong.integration.activity.model.BaseActivityShop;
import com.mfangsoft.zhuangjialong.integration.activity.service.ActivityService;
import com.mfangsoft.zhuangjialong.integration.customer.model.Customer;
import com.mfangsoft.zhuangjialong.integration.gift.model.BaseGift;
import com.mfangsoft.zhuangjialong.integration.order.model.BaseOrder;
import com.mfangsoft.zhuangjialong.integration.permission.model.PermissionEntity;
import com.mfangsoft.zhuangjialong.integration.statisticsactivity.model.BaseStatisticsActivity;
import com.mfangsoft.zhuangjialong.integration.statisticsactivity.service.StatisticsActivityService;
import com.mfangsoft.zhuangjialong.integration.user.model.UserEntity;
import com.mfangsoft.zhuangjialong.integration.user.service.UserService;



@Controller
@RequestMapping("/app")
public class HelperActivityController {
	
	@Autowired
	private ActivityService activityService;
	

	
	
	/**
	 * 助手活动列表
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectHelperActivityPage",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin 
	public ResponseMessage<Page<Map<String, Object>>> selectHelperActivityPage(@RequestBody Page<Map<String, Object>> page) throws ParseException{
		ResponseMessage<Page<Map<String, Object>>> message = new ResponseMessage<Page<Map<String, Object>>>();
		 
		    message.setResult(activityService.selectHelperActivityPage(page));
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		return message;
	}

	/**
	 * 助手往期活动列表
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectHelperHistoryActivityPage",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin 
	public Page<Map<String, Object>> selectHelperHistoryActivityPage(@RequestBody Page<Map<String, Object>> page) throws ParseException{    
			
		return activityService.selectHelperHistoryActivityPage(page);
	}
	/**
	 * 活动流程
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectHelperActivityUser",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<Map <String,Object>> selectActivityUser(@RequestBody BaseEntryActivity baseEntryActivity) throws ParseException{
		
		ResponseMessage<Map <String,Object>> message = new ResponseMessage<Map <String,Object>>();
		try {
			message.setResult(activityService.selectHelperActivityUser(baseEntryActivity));
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	/**
	 * 签到
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/uptdateSign",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<BaseEntryActivity> uptdateSign(@RequestBody BaseEntryActivity baseEntryActivity) throws ParseException{
		
		ResponseMessage<BaseEntryActivity> message = new ResponseMessage<BaseEntryActivity>();
		try {
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			activityService.uptdateSign(baseEntryActivity);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	/**
	 * 确定购买 卡
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/uptdateCard",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<BaseEntryActivity> uptdateCard(@RequestBody BaseEntryActivity baseEntryActivity) throws ParseException{
		
		ResponseMessage<BaseEntryActivity> message = new ResponseMessage<BaseEntryActivity>();
		try {
			baseEntryActivity.setBuyCard_time("1");
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			activityService.uptdateCard(baseEntryActivity);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	/**
	 * 签到页面查看
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectSignGo",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<BaseActivityInfo> selectSignGo(@RequestBody BaseActivityInfo baseActivityInfo,HttpServletRequest request, HttpServletResponse response) throws ParseException{
		
		ResponseMessage<BaseActivityInfo> message = new ResponseMessage<BaseActivityInfo>();
		try {
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(activityService.selectSignGo(baseActivityInfo));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	/**
	 * 助手活动详情   联单活动副本内容       小程序首页
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectHelperActivityAll",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<Map <String,Object>> selectHelperActivityAll(@RequestBody BaseActivityInfo baseActivityInfo) throws ParseException{
		
		ResponseMessage<Map <String,Object>> message = new ResponseMessage<Map <String,Object>>();
		try {
			/*BaseActivityInfo baseActivityInfo =new BaseActivityInfo();
			baseActivityInfo.setId(58);*/
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(activityService.selectHelperActivityAll(baseActivityInfo));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	/**
	 * 小程序判断是否 报名
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectSigoName",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<Map <String,Object>> selectSigoName(@RequestBody BaseActivityInfo baseActivityInfo) throws ParseException{
		
		ResponseMessage<Map <String,Object>> message = new ResponseMessage<Map <String,Object>>();
		try {
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(activityService.selectSigoName(baseActivityInfo));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	/**
	 * 活动管理   活动客户
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectHelperActivityManage",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<BaseEntryActivity> selectHelperActivityManage(@RequestBody BaseEntryActivity baseEntryActivity) throws ParseException{
		
		ResponseMessage<BaseEntryActivity> message = new ResponseMessage<BaseEntryActivity>();
		try {
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(activityService.selectHelperActivityManage(baseEntryActivity));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	/**
	 * 活动管理   活动订单
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectHelperAllActivityOrder",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<BaseOrder> selectHelperAllActivityOrder(@RequestBody BaseOrder baseOrder) throws ParseException{
		
		ResponseMessage<BaseOrder> message = new ResponseMessage<BaseOrder>();
		try {
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(activityService.selectHelperAllActivityOrder(baseOrder));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	/**
	 * 礼品核销
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/uptdateHelperActivityOrder",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<BaseOrder> uptdateHelperActivityOrder(@RequestBody OrderGift orderGift) throws ParseException{
		
		ResponseMessage<BaseOrder> message = new ResponseMessage<BaseOrder>();
		try {
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			activityService.uptdateHelperActivityOrder(orderGift);
			
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	/**
	 * 礼品使用
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/uptdateHelperActivityUseOrder",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<BaseOrder> uptdateHelperActivityUseOrder(@RequestBody OrderGift orderGift) throws ParseException{
		
		ResponseMessage<BaseOrder> message = new ResponseMessage<BaseOrder>();
		try {
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			activityService.uptdateHelperActivityOrder(orderGift);
			
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	/**
	 * 助手账户管理中  的  活动详情
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectHelperActivityDetails",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<Map <String,Object>> selectHelperActivityDetails(@RequestBody BaseActivityInfo baseActivityInfo) throws ParseException{
		
		ResponseMessage<Map <String,Object>> message = new ResponseMessage<Map <String,Object>>();
		try {
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(activityService.selectHelperActivityDetails(baseActivityInfo));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	
	/**
	 * 小程序   活动详情
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectXCXActivity",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<Map <String,Object>> selectXCXActivity(@RequestBody BaseActivityInfo baseActivityInfo) throws ParseException{
		
		ResponseMessage<Map <String,Object>> message = new ResponseMessage<Map <String,Object>>();
		try {
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(activityService.selectXCXActivity(baseActivityInfo));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	/**
	 * 小程序   报名页面
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectXCXSigo",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<Map <String,Object>> selectXCXSigo(@RequestBody Customer customer) throws ParseException{
		
		ResponseMessage<Map <String,Object>> message = new ResponseMessage<Map <String,Object>>();
		try {
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(activityService.selectXCXSigo(customer));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	
	/**
	 * 小程序   往期活动
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectXCXHistoryActivity/{shop_id}",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<List<Map <String,Object>>> selectXCXHistoryActivity(@PathVariable Integer shop_id) throws ParseException{
		
		ResponseMessage<List<Map <String,Object>>> message = new ResponseMessage<List<Map <String,Object>>>();
		try {
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(activityService.selectXCXHistoryActivity(shop_id));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}
	/**
	 * 小程序店铺
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectXCXHistoryActivityShop",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<List<Map <String,Object>>> selectXCXHistoryActivityShop(@RequestBody Map <String,Object> map) throws ParseException{
		
		ResponseMessage<List<Map <String,Object>>> message = new ResponseMessage<List<Map <String,Object>>>();
		try {
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(activityService.selectXCXHistoryActivityShop(map));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}

	/**
	 * 小程序  获取活动id  列表
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectXCXHistoryActivityID/{shop_id}",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<List<Map <String,Object>>> selectXCXHistoryActivityID(@PathVariable Integer shop_id) throws ParseException{
		
		ResponseMessage<List<Map <String,Object>>> message = new ResponseMessage<List<Map <String,Object>>>();
		try {
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(activityService.selectXCXHistoryActivityID(shop_id));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}

	/**
	 * 小程序   最近店铺
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/minshop",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<Map<String,Object>> minShop(@RequestBody Map <String,Object> map) throws ParseException{
		
		ResponseMessage<Map <String,Object>> message = new ResponseMessage<Map <String,Object>>();
		try {
			
			Map<String,Object> resultMap=activityService.selectMinShopByLBS(map);
			
			if(!resultMap.isEmpty()){
				
				message.setCode(SysConstant.SUCCESS_CODE);
				message.setMessage(SysConstant.SUCCESS_MSG);
				message.setResult(resultMap);
			}else{
				message.setCode(SysConstant.FAILURE_CODE);
				message.setMessage(SysConstant.FAILURE_MSG);
				
			}
		} catch (Exception e) {
			
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}

	
	/**
	 * 小程序  通过用户ID 获取活动
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectXCXHistoryActivityIDTwo/{customer_id}",method=RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public  ResponseMessage<List<Map <String,Object>>> selectXCXHistoryActivityIDTwo(@PathVariable Integer customer_id) throws ParseException{
		
		ResponseMessage<List<Map <String,Object>>> message = new ResponseMessage<List<Map <String,Object>>>();
		try {
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
			message.setResult(activityService.selectXCXHistoryActivityIDTwo(customer_id));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	}

}
